﻿using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.BLL.RecordCenter;
using System.Linq;
using MS.InformationManagement.Support;
//using MS.InformationManagement.S

namespace MS.InformationManagement.Presenter.TRDConfigGrid
{
    public class TRDConfigGridPresenter : BasePresenter<ITRDConfigGridView>
    {
        public TRDConfigGridPresenter(ITRDConfigGridView view) : base(view) { }

        public void LoadConfiguredItems()
        {

        }

        public void HandlerLoad()
        {
            View.Sections = TRDBLL.LoadSections();
            View.Backdrops = TRDBLL.LoadParentBackdrops();

            View.Backdrop = View.Backdrops.First();
            ReloadNonConfiguredSections();
        }

        public void LoadBackdropChilds()
        {
            View.ChildBackdrops = TRDBLL.LoadBackdropChilds(View.Backdrop.Id.Value);
        }

        public void LoadSubsections()
        {
            View.Subsections = TRDBLL.LoadSubsections(View.Section.Id.Value);
        }

        public void LoadTRDConfigItems()
        {
            int trdTotalRecords = 0;
            int historyTotalRecords = 0;
            int nonConfiguredTotalRecords = 0;

            View.TRDConfigItems = TRDBLL.GetTRDConfigItems(View.Backdrop, View.ChildBackdrop, View.Section, View.Subsection, View.TrdItemsPageIndex, View.TrdItemsPageSize, out trdTotalRecords);
            View.TrdItemsTotalRecords = trdTotalRecords;

            View.HistoricTRDConfigItems = TRDBLL.GetHistoricItems(View.Backdrop, View.ChildBackdrop, View.Section, View.Subsection, View.HistoryItemsPageIndex, View.HistoryItemsPageSize, out historyTotalRecords);
            View.HistoryItemsTotalRecords = historyTotalRecords;

            View.NonConfiguredSections = TRDBLL.GetPendingSections(View.NonConfiguredSectionsPageIndex, View.NonConfiguredSectionsPageSize, out nonConfiguredTotalRecords);
            View.NonConfiguredSectionsTotalRecords = nonConfiguredTotalRecords;
        }

        public void ReloadTRDConfigItems()
        {
            int trdTotalRecords = 0;
            View.TRDConfigItems = TRDBLL.GetTRDConfigItems(View.Backdrop, View.ChildBackdrop, View.Section, View.Subsection, View.TrdItemsPageIndex, View.TrdItemsPageSize, out trdTotalRecords);
            View.TrdItemsTotalRecords = trdTotalRecords;
        }

        public void ReloadHistoryItems()
        {
            int historyTotalRecords = 0;
            View.HistoricTRDConfigItems = TRDBLL.GetHistoricItems(View.Backdrop, View.ChildBackdrop, View.Section, View.Subsection, View.HistoryItemsPageIndex, View.HistoryItemsPageSize, out historyTotalRecords);
            View.HistoryItemsTotalRecords = historyTotalRecords;
        }

        public void ReloadNonConfiguredSections()
        {
            int nonConfiguredTotalRecords = 0;
            View.NonConfiguredSections = TRDBLL.GetPendingSections(View.NonConfiguredSectionsPageIndex, View.NonConfiguredSectionsPageSize, out nonConfiguredTotalRecords);
            View.NonConfiguredSectionsTotalRecords = nonConfiguredTotalRecords;
        }

        public void ExportPendingSections()
        {
            string pendingsectionsFileContent = CsvExportHelper.ExportToCSV(TRDBLL.GetPendingSectionsExportData(), new string[] { "Codigo", "Seccion", "Subseccion", "Usuario Encargado Correspondencia" }, ';');
            View.ExportToCsv("SeccionesPendientes", pendingsectionsFileContent);
        }

        public void ExportTRDItems()
        {
            string trditemsFileContent = CsvExportHelper.ExportToCSV(TRDBLL.GetTRDConfigItemsExportData(View.Backdrop, View.ChildBackdrop, View.Section, View.Subsection),
                new string[] { "D", "S", "SB", "Serie", "Subserie", "Tipos Documentales", "AG", "AC", "C", "MT", "S", "E", "Observaciones" }, ';');
            View.ExportToCsv(string.Format("TRD_{0}_{1}", View.Section.Title, View.Subsection.Title), trditemsFileContent);
        }

        public void ExportTRDHistory()
        {
            string trditemsFileContent = CsvExportHelper.ExportToCSV(TRDBLL.GetHistoricItemsExportData(View.Backdrop, View.ChildBackdrop, View.Section, View.Subsection),
                new string[] { "Fecha", "Administrador", "Serie", "Subserie", "Operacion Realizada", "Observaciones" }, ';');
            View.ExportToCsv(string.Format("HistoricoTRD_{0}_{1}", View.Section.Title, View.Subsection.Title), trditemsFileContent);
        }
    }
}
